﻿using System;
using System.Collections.Generic;
using System.Text;
using Common;
using System.Reflection;

namespace User
{
    public partial class ClientManager
    {
        public LoginServerConnection login_server_connection;
        public TableConnection table_connection;
        public ClientManager()
        {
            login_server_connection = new LoginServerConnection();
            login_server_connection.Receive_From_Login_Server += new EventHandler<User.YEventArgs.ReceiveEventArgs>(login_server_connection_Receive_From_Login_Server);
            table_connection = new TableConnection();
            table_connection.Receive_From_Table += new EventHandler<User.YEventArgs.ReceiveEventArgs>(table_connection_Receive_From_Table);
        }       

        void login_server_connection_Receive_From_Login_Server(object sender, User.YEventArgs.ReceiveEventArgs e)
        {
            MSG_GSM message = (MSG_GSM)e.data[0];
            Console.WriteLine("Receive: " + message.ToString());
            List<byte> param = new List<byte>();
            for (int i = 1; i < e.data.Length; i++)
            {
                param.Add((byte)e.data[i]);
            }

            try
            {
                Type t = typeof(ClientManager);
                MethodInfo handler = t.GetMethod("Process_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
                handler.Invoke(this, new object[] { param.ToArray() });
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Error MSG_GSM." + message.ToString() + "  " + ex.Message);
            }            
        }

        void table_connection_Receive_From_Table(object sender, User.YEventArgs.ReceiveEventArgs e)
        {
            YMSG message = (YMSG)e.data[0];
            Console.WriteLine("Receive: " + message.ToString());
            List<byte> param = new List<byte>();
            for (int i = 1; i < e.data.Length; i++)
            {
                param.Add((byte)e.data[i]);
            }

            try
            {
                Type t = typeof(ClientManager);
                MethodInfo handler = t.GetMethod("Process_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
                handler.Invoke(this, new object[] { param.ToArray() });
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Error MSG_GSM." + message.ToString() + "  " + ex.Message);
            }       
        }

    }
}
